package com.spic.business.alarm.controller;

import com.spic.business.alarm.domain.AlarmDevice;
import com.spic.business.alarm.service.IAlarmDeviceService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 告警桩Controller
 *
 * @author spic
 * @date 2022-07-01
 */
@RestController
@RequestMapping("/alarmDevice")
public class AlarmDeviceController extends BaseController {
    @Autowired
    private IAlarmDeviceService alarmDeviceService;

    /**
     * 查询告警桩列表
     */
    @PreAuthorize(hasPermi = "business:alarmDevice:list")
    @GetMapping("/list")
    public TableDataInfo list(AlarmDevice alarmDevice) {
        startPage();
        List<AlarmDevice> list = alarmDeviceService.selectAlarmDeviceList(alarmDevice);
        return getDataTable(list);
    }

    /**
     * 导出告警桩列表
     */
    @PreAuthorize(hasPermi = "business:alarmDevice:export")
    @Log(title = "告警桩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlarmDevice alarmDevice) throws IOException {
        List<AlarmDevice> list = alarmDeviceService.selectAlarmDeviceList(alarmDevice);
        ExcelUtil<AlarmDevice> util = new ExcelUtil<AlarmDevice>(AlarmDevice.class);
        util.exportExcel(response, list, "告警桩数据");
    }

    /**
     * 获取告警桩详细信息
     */
    @PreAuthorize(hasPermi = "business:alarmDevice:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(alarmDeviceService.selectAlarmDeviceById(id));
    }

    /**
     * 新增告警桩
     */
    @PreAuthorize(hasPermi = "business:alarmDevice:add")
    @Log(title = "告警桩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AlarmDevice alarmDevice) {
        return toAjax(alarmDeviceService.insertAlarmDevice(alarmDevice));
    }

    /**
     * 修改告警桩
     */
    @PreAuthorize(hasPermi = "business:alarmDevice:edit")
    @Log(title = "告警桩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlarmDevice alarmDevice) {
        return toAjax(alarmDeviceService.updateAlarmDevice(alarmDevice));
    }

    /**
     * 删除告警桩
     */
    @PreAuthorize(hasPermi = "business:alarmDevice:remove")
    @Log(title = "告警桩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(alarmDeviceService.deleteAlarmDeviceByIds(ids));
    }
}
